package com.pro.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.domain.User;
import com.pro.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @Override
    public String Redisyzm() {
        String yzm = stringRedisTemplate.opsForValue().get("yanzhengma");
        System.out.println("从redis中取出了验证码："+yzm);
        return yzm;
    }

    /*用户登录，输入手机号和验证码，如果数据库里没有该手机号码，就添加这个手机号，再验证验证码；
    数据库里如果有，就将输入的验证码和redis中存的相比较，正确就登录成功，不正确就登录失败*/
    @Override
    public Map LoginCheck(String mobile, String yanzhengma) {
        Map map = new HashMap();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        String redisyzm = userService.Redisyzm();
        /*获得所有用户信息*/
        User user = userMapper.selectOne(queryWrapper);
        map.put("LoginUser",user);
        if (user == null) {
            User user1 = new User();
            user1.setMobile(mobile);
            userMapper.insert(user1);
            if (yanzhengma.equals(redisyzm)) {
                map.put("msg","手机号注册成功！验证码正确！登陆成功！");
                map.put("code",200);
                return map;
            } else {
                map.put("msg","验证码错误！登陆失败！请重新发送验证码！");
                map.put("code",300);
                return map;
            }
        }else {
            if (yanzhengma.equals(redisyzm)) {
                map.put("msg","验证码正确！登陆成功！");
                map.put("code",200);
                return map;
            } else {
                map.put("msg","验证码错误！登陆失败！请重新发送验证码！");
                map.put("code",300);
                return map;
            }
        }
        }
    }
